<template>
	<view class="pageBg" style="min-height: 100%;">
		<Skeleton v-if="skeletonLoading" type="list"></Skeleton>
		<Head type="jobList" title="找工作" :isBack="false">
			<ListFilters ref="refListFilters" :params="params" otherFilterType="jobListType"
				@doSearchByDistrict="doSearchByDistrict" @doSearchByCategory="doSearchByCategory"
				@doSearchByOther="doSearchByOther">
				<view class="tabBtns">
					<view :class="currentTab == 1 ? 'btn ac' : 'btn'" @click="changeTab(1)">综合</view>
					<view :class="currentTab == 2 ? 'btn ac' : 'btn'" @click="changeTab(2)">附近</view>
				</view>
			</ListFilters>
		</Head>
		<view class="jobList">
			<jobItem v-for="(item,index) in dataset" :key="item.id" :item="item" @toDetail="toDetail"></jobItem>
			<u-loadmore v-if="dataset.length >= pagesize" class="u-loadmore" :status="finished_status"></u-loadmore>
			<Empty v-if="show_empty"></Empty>
		</view>
		<!-- 未授权弹窗 -->
		<Popup ref="authPopup" @confirm="handleAuthConfirm">
			<view style="padding:20rpx 0;">{{auth.text}}</view>
		</Popup>
		<!-- <PrivacySettingPopup ref="refPrivacySettingPopup" :sitename="config.sitename" :showPrivacy="showPrivacy"
			:privacyContractName="privacyContractName"></PrivacySettingPopup> -->
		<BottomNav></BottomNav>
	</view>
</template>

<script>
	import http from '@/utils/http'
	import api from '@/api'
	import {
		gcToDb
	} from '@/utils/map.js'

	import ListFilters from '@/components/ListFilters.vue'
	import jobItem from '@/components/listItem/jobItem.vue'
	import Skeleton from '@/components/Skeleton.vue'
	import PrivacySettingPopup from '@/components/PrivacySettingPopup.vue'
	import {
		onShareAppMessage,
		onShareTimeline
	} from '@/utils/share.js'
	import {
		getAllPrivacySetting
	} from '@/utils/index.js'
	import {
		changeIsAuth
	} from '@/utils/uni.js'
	import {
		mapState
	} from 'vuex'
	export default {
		name: 'JobList',
		components: {
			jobItem,
			ListFilters,
			Skeleton
			// PrivacySettingPopup
		},
		data() {
			return {
				selectArrOnload: [],
				skeletonLoading: true,
				currentTab: 1,
				finished_status: 'loadmore',
				page: 1,
				pagesize: 15,
				dataset: [],
				params: {
					search_type: 'list',
					keyword: '',
					district1: '',
					district2: '',
					district3: '',
					category1: '',
					category2: '',
					category3: '',
					experience: '',
					minwage: '',
					maxwage: '',
					filter_apply: '',
					nature: '',
					education: '',
					tag: '',
					settr: '',
					districtName: '地区',
					categoryName: '职位',
					otherNum: 0,
					wage: '',
				},
				show_empty: false,
				auth: {
					text: '',
					authText: '',
					isAuth: ''
				},
				showPrivacy: false,
				privacyContractName: ''
			}
		},
		computed: {
			...mapState(['config'])
		},
		onLoad(options) {
			// let privacySettingObj = JSON.parse(uni.getStorageSync('privacySettingObj')) 
			// this.showPrivacy = true //privacySettingObj.showPrivacy
			// this.privacyContractName = privacySettingObj.privacyContractName
			if (options && options.paramsCurrent && options.paramsCurrent != '') {
				this.params = JSON.parse(options.paramsCurrent)
				this.$refs.refListFilters.initShareData(this.params)
				this.fetchData(true)
			}
		},
		onShareAppMessage() {
			return onShareAppMessage({
				url: '/views/JobList?paramsCurrent=' + JSON.stringify(this.params),
				alias: "joblist",
				info: {}
			})
		},
		onShareTimeline() {
			// info.imgUrl = this.info.thumb
			return onShareTimeline({
				url: '/views/JobList?paramsCurrent=' + JSON.stringify(this.params),
				alias: "joblist",
				info: {}
			})
		},
		onPageScroll(res) {
			uni.$emit('onPageScroll', res.scrollTop)
		},
		onReachBottom() {
			this.finished_status = 'loading';
			this.page++
			this.fetchData(false)
		},
		mounted() {
			// 请求列表数据
			this.fetchData(true)
		},
		methods: {
			handleAuthConfirm() {
				var {
					authText,
					isAuth
				} = this.auth
				if (authText == 'gps') {
					this.$refs.authPopup.close()
					// this.changeIsAuth()
				} else if (authText == 'setting') {
					uni.openSetting({
						success: (res) => {}
					})
				}
			},
			toDetail(id) {
				let selectArr = uni.getStorageSync('selectArr')
				selectArr = selectArr == 'null' || selectArr == null || selectArr == '' ? [] : JSON.parse(selectArr)
				if (selectArr.indexOf(id) == -1 || selectArr.length == 0) {
					selectArr.push(id)
				}
				this.selectArrOnload = selectArr
				uni.setStorageSync('selectArr', JSON.stringify(selectArr))
				for (let i in this.dataset) {
					if (this.dataset[i].id == id) {
						this.dataset[i].clicked = true
					}
				}
				uni.navigateTo({
					url: '/commonViews/JobShow?id=' + id
				})
			},
			// 其他搜索
			doSearchByOther(data) {
				this.doSearch(data)
			},
			// tab切换
			async changeTab(type) {
				var that = this
				// type 1综合 2 附近
				if (type == 1) {
					this.params.lat = 0
					this.params.lng = 0
					this.fetchData(true)
					this.currentTab = type
				} else if (type == 2) {
					let {
						auth,
						isAuth,
						isGps,
						location
					} = await changeIsAuth('location')
					if (!isGps) {
						//gps权限没有开启 提示用户开启权限
						this.auth.text = '请打开定位服务功能'
						this.auth.authText = 'gps'
						this.auth.isAuth = isGps
						this.$refs.authPopup.open()
					} else if (!isAuth) {
						this.auth.text = '请允许使用位置信息'
						this.auth.authText = 'setting'
						this.auth.isAuth = isAuth
						this.$refs.authPopup.open()
					} else {
						var localtion2 = gcToDb(location.latitude, location.longitude)
						this.params.lat = localtion2.lat
						this.params.lng = localtion2.lng
						this.fetchData(true)
					}
					if (isAuth) {
						this.currentTab = type
					}
				}


			},
			// 请求列表数据，init为true时直接更改dataset值，false时代表上拉加载回的数据追加进dataset
			fetchData(init) {
				this.show_empty = false
				let conditions = {
					...this.params
				}
				if (init === true) {
					this.page = 1
					this.finished_status = 'loading'
				}
				conditions.page = this.page
				conditions.pagesize = this.pagesize
				http
					.get(api.joblist, conditions)
					.then(res => {
						if (init === true) {
							this.dataset = [...res.data.data.items]
						} else {
							this.dataset = this.dataset.concat(res.data.data.items)
						}

						// 判断用户是否点击查看过某些数据
						if (this.selectArrOnload.length > 0) {
							for (let j in this.selectArrOnload) {
								for (let i in this.dataset) {
									if (this.dataset[i].id == this.selectArrOnload[j]) {
										this.dataset[i].clicked = true
									} else {
										this.dataset[i].clicked = false
									}
								}
							}
						} else {
							for (let i in this.dataset) {
								this.dataset[i].clicked = false
							}
						}
						this.skeletonLoading = false
						this.finished_status = 'loading';
						// 数据全部加载完成
						if (res.data.data.items.length <= this.pagesize) {
							this.finished_status = 'nomore'
							if (init === false) {
								this.finished_status = 'nomore'
							} else if (res.data.data.items.length === 0) {
								this.show_empty = true
								this.finished_status = 'nomore'
							}
						}
					})
					.catch(() => {})
			},
			// 地区筛选
			doSearchByDistrict(data) {
				this.params.districtName = data.districtName
				this.params.district1 = data.district1
				this.params.district2 = data.district2
				this.params.district3 = data.district3
				this.doSearch({
					district1: data.district1,
					district2: data.district2,
					district3: data.district3
				})
			},
			// 搜索通用函数
			doSearch(data) {
				for (const key in data) {
					if (this.params.hasOwnProperty(key)) {
						this.params[key] = data[key]
					}
				}
				if (data.otherNum == 0) {
					this.params.tag = ''
				}
				this.page = 1
				this.fetchData(true)
			},
			// 职位分类筛选
			doSearchByCategory(data) {
				this.params.categoryName = data.categoryName
				this.params.category1 = data.district1
				this.params.category2 = data.district2
				this.params.category3 = data.district3
				this.doSearch({
					category1: data.category1,
					category2: data.category2,
					category3: data.category3
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.jobList {
		padding: 0 30rpx;
	}
</style>